package tt.dz.biz.service.common;

import com.alibaba.fastjson.JSONObject;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.common.CommonDAO;
import tt.dz.biz.dao.merch.ChangeDayDAOImp;
import tt.dz.common.DZStringUtil;
import tt.dz.common.DateUtil;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.bizSysCode;
import tt.dz.vo.CityVO;
import tt.dz.vo.CountryVO;
import tt.dz.vo.DistrictVO;
import tt.dz.vo.ProvinceVO;
import tt.dz.vo.object.Node;

@Service("commonService")
public class CommonService
{
  @Resource(name="commonDAO")
  private CommonDAO commonDAO;
  @Resource(name="changeDayDAO")
  private ChangeDayDAOImp changeDayDAOImp;
  @Value("${downLoad.dir}")
  private String downLoadDir;
  
  public String getDownLoadDir()
  {
    return this.downLoadDir;
  }
  
  public void setDownLoadDir(String downLoadDir)
  {
    this.downLoadDir = downLoadDir;
  }
  
  public Date getAccountingDate()
  {
    return this.changeDayDAOImp.queryAccountingDateForChangeDay();
  }
  
  public List<CountryVO> listAllCountry()
  {
    return this.commonDAO.listAllCountry();
  }
  
  public List<ProvinceVO> listAllProvince()
  {
    return this.commonDAO.listAllProvince();
  }
  
  public List<CityVO> listAllCity()
  {
    return this.commonDAO.listAllCity();
  }
  
  public List<DistrictVO> listAllDistrict()
  {
    return this.commonDAO.listAllDistrict();
  }
  
  public JSONObject listNextDistrictByDistrictCode(String districtCode, String level)
    throws Exception
  {
    return this.commonDAO.listNextDistrictByDistrictCode(districtCode, level);
  }
  
  public Node searchAllClassificationAndAssembly()
    throws Exception
  {
    return this.commonDAO.searchAllClassificationAndAssembly();
  }
  
  public Date getDBCurrentTime()
  {
    return this.commonDAO.getDBCurrentTime();
  }
  
  public String getCurrentSysState()
  {
    return this.commonDAO.getCurrentSysState(DzConstants.bizSysCode.MAIN_SERVER.value());
  }
  
  public long querySequence(String seqName)
  {
    return this.commonDAO.querySequence(seqName).longValue();
  }
  
  public String getTradeNo(String userType, String sysCode)
  {
    String currentDate = DateUtil.getDateFormatter();
    String seqNo = DZStringUtil.getFixedLengthString(8, String.valueOf(querySequence("seq_trade_no")));
    if (StringUtils.isBlank(userType)) {
      userType = "0";
    }
    if (StringUtils.isBlank(sysCode)) {
      sysCode = "000";
    }
    String tradeNo = userType + sysCode + currentDate + seqNo;
    return tradeNo;
  }
  
  public String getSubTradeNo()
  {
    String currentDate = DateUtil.getDateFormatter();
    String subSeqNo = DZStringUtil.getFixedLengthString(8, 
      String.valueOf(querySequence("seq_trade_sub_no")));
    return currentDate + subSeqNo;
  }
}
